﻿
namespace SanguoSharp.Server.Cards
{
    public sealed class 决斗 : TargetPlayerCard
    {
        private class DuelResponseHandler : ResponseHandler, IResultHandler
        {
            private 决斗 card;
            private Player targetPlayer;
            private Player owner;
            private Player current;

            public DuelResponseHandler(决斗 card, Player targetPlayer, Player owner)
                : base(targetPlayer, owner)
            {
                this.card = card;
                this.targetPlayer = targetPlayer;
                this.owner = owner;
            }

            protected override void OnStart()
            {
                current = targetPlayer;
                PushHandler(new ThrowKillResponseHandler(current, owner, this));
            }

            protected override void OnContinue()
            {
                End();
            }

            private void NextPlayer()
            {
                current = current == targetPlayer ? owner : targetPlayer;
            }

            void IResultHandler.OnResult(bool result)
            {
                if (result)
                {
                    NextPlayer();
                    PushHandler(new ThrowKillResponseHandler(current, owner, this));
                }
            }
        }

        public 决斗(Game game, int id, CardSuit suit, CardRank rank)
            : base(game, id, CardType.决斗, suit, rank, RequestType.DuelTarget)
        {
        }

        protected override void OnPlay(Player owner, Player targetPlayer)
        {
            Game.GameCycle.PushTempHandler(new DuelResponseHandler(this, targetPlayer, owner));
        }
    }
}

